package com.example.tliasmanagement.controller;


import com.example.tliasmanagement.vo.Result2;
import com.itheima.aliyun.AliyunOSSUtils;
import com.itheima.qiniu.QiniuKodoUtlis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {

    //    @Value("${aliyun.oss.endpoint}")
//    private String endpoint;
//
//    @Value("${aliyun.oss.bucket}")
//    private String bucket;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;
@Autowired
private QiniuKodoUtlis qiniuKodoUtlis;
    @PostMapping("/upload")
    public Result2<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        // 拿到上传的文件的后缀
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 拿到上传的文件的字节数组
        byte[] content = file.getBytes();
        // 调用util上传到aliyun oss
        String url = aliyunOSSUtils.upload(content, extName);
        // 调用util上传到qiniuyun oss
        //String url = QiniuKodoUtlis .upload( content, extName);
        // 返回url地址
        return Result2.ok(url);
    }
}

//    @PostMapping("/upload")
//    public String upload(String username,
//                         Integer age,
//                         @RequestParam("file") MultipartFile file) throws Exception {
//        // 获取上传的文件名
//        String originalFilename = file.getOriginalFilename();
//        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
//        String newFileName = UUID.randomUUID().toString() + "." + suffix;
//        //获取的表单项的名字
//        String name = file.getName();
//        log.info("username:{},age:{},originalFilename:{}", username, age, originalFilename);
//        //获取上传的文件的输入流
//        //InputStream inputStream = file.getInputStream();
//        File dst = new File("d:/", newFileName);
//        file.transferTo(dst);
//        //return "success";
//        return "http://localhost:8080/download?filename=" + newFileName;
//    }
//
//    @GetMapping("/download")
//    public void download(String filename, HttpServletResponse response) throws Exception {
//        // 设置响应数据的类型
//        response.setContentType("image/png");
//        // 构造要下载的文件
//        File dst = new File("d:/", filename);
//        // 获取文件的输入流
//        FileInputStream fileInputStream = new FileInputStream(dst);
//        // 获取响应的输出流
//        ServletOutputStream outputStream = response.getOutputStream();
//        IOUtils.copyLarge(fileInputStream, outputStream);
//        // 关闭资源
//        fileInputStream.close();
//        outputStream.close();
//    }

